package com.woniuxy.amusementparkos.controller.xxy;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.amusementparkos.dto.lvd.TableDataDto;
import com.woniuxy.amusementparkos.entity.xxy.Store_Info;
import com.woniuxy.amusementparkos.entity.xxy.User_Store;
import com.woniuxy.amusementparkos.service.xxy.Store_UserService;
import com.woniuxy.amusementparkos.util.lzw.result.ResponseResult;

@Controller
public class UserController {
	@Autowired
	Store_UserService store_UserService;
	
	@RequestMapping("/store/user/regist")
	@ResponseBody
	public ResponseResult<String> regist(String user_id ,String password ,String store_key,HttpSession session) {
			
		int store_id = (int) session.getAttribute("store_id");
			User_Store user = new User_Store();
			Store_Info store_Info = new Store_Info();
			store_Info.setStore_key(store_key);
			store_Info.setStore_id(store_id);
			user.setUser_id(Integer.parseInt(user_id));
			user.setPassword(password);
			user.setStore_Info(store_Info);
		try {
			store_UserService.regist(user);
			return new ResponseResult<String>(0, "新增成功", null);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
	}
	//密码修改
	@RequestMapping("/store/user/updatePassword")
	@ResponseBody
	public ResponseResult<String> updatePassword(String user_id ,String password_old ,String password_new) {
			try {
				store_UserService.updatePassword(Integer.parseInt(user_id), password_old, password_new);
				return new ResponseResult<String>(0, "修改成功", null);
				
			} catch (Exception e) {
				return new ResponseResult<String>(500, e.getMessage(), null);
			}
		
	}
	@RequestMapping("/store/user/loginByKey")
	@ResponseBody
	public ResponseResult<String> loginByKey(String store_key,HttpSession session) {
		try {
			Store_Info store = store_UserService.loginByKey(store_key);
			session.setAttribute("store_id", store.getStore_id());
			return new ResponseResult<String>(0, "登陆成功,您使用的是商家码登录,请尽快前往账号管理页面创建账号,或者更改您忘记的密码", null);
		} catch (Exception e) {
			return  new ResponseResult<String>(500, "登陆失败:"+e.getMessage(), null);
		}
	}
	@RequestMapping("/store/user/selectAllUsers")
	@ResponseBody
	public TableDataDto<User_Store> selectAllUsersByStoreId(HttpSession session) {
		int store_id =  (int) session.getAttribute("store_id");
		try {
			List<User_Store> users = store_UserService.selectUser_StreByStore_Id(store_id);
			long count = store_UserService.selectUser_StreByStore_IdCount(store_id);
			return new TableDataDto<User_Store>(0, "查询成功", count, users);
		} catch (Exception e) {
			return new TableDataDto<>(500, e.getMessage(), null, null);
		}
		
	}
	@RequestMapping("/store/user/deleteUser")
	@ResponseBody
	public ResponseResult<String> deleteUser(String store_key,String user_id) {
		try {
			store_UserService.deleteUser(user_id, store_key);
			return new ResponseResult<String>(0, "删除成功", null);
		} catch (Exception e) {
			return  new ResponseResult<String>(500, e.getMessage(), null);
		}
		
		
	}
	@RequestMapping("/store/user/login")
	@ResponseBody
	public ResponseResult<String> login(User_Store user,HttpSession session) {
		System.out.println(user);
		try {
			 user = store_UserService.login(user);
			session.setAttribute("store_id", user.getStore_Info().getStore_id());
			return new ResponseResult<String>(0, "登陆成功", null);
		} catch (Exception e) {
			return new ResponseResult<String>(500, e.getMessage(), null);
		}
		
		
	}
	///store/user/logout
	@RequestMapping("/store/user/logout")
	public String logout(HttpSession session) {
		session.removeAttribute("store_id");
		return "xxy/商家登陆页";
	}
	
	//以下为页面跳转
	@RequestMapping("/store/user/storeLogin")
	public String storeLogin() {
		return "xxy/商家登陆页";
	}
	@RequestMapping("/store/user/getUsers")
	public String getUsers() {
		return "xxy/账号列表页";
	}
	@RequestMapping("/toMain")
	public String toMain() {
		return "store_manager";
	}
	
}
